<html><!-- Created using the cpp_pretty_printer from the dlib C++ library.  See http://dlib.net for updates. --><head><title>dlib C++ Library - hough_transform_ex.cpp</title></head><body bgcolor='white'><pre>
<font color='#009900'>// The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt
</font><font color='#009900'>/*

    This is an example illustrating the use of the Hough transform tool in the
    dlib C++ Library.


    In this example we are going to draw a line on an image and then use the
    Hough transform to detect the location of the line.  Moreover, we do this in
    a loop that changes the line's position slightly each iteration, which gives
    a pretty animation of the Hough transform in action.
*/</font>

<font color='#0000FF'>#include</font> <font color='#5555FF'>&lt;</font>dlib<font color='#5555FF'>/</font>gui_widgets.h<font color='#5555FF'>&gt;</font>
<font color='#0000FF'>#include</font> <font color='#5555FF'>&lt;</font>dlib<font color='#5555FF'>/</font>image_transforms.h<font color='#5555FF'>&gt;</font>

<font color='#0000FF'>using</font> <font color='#0000FF'>namespace</font> dlib;

<font color='#0000FF'><u>int</u></font> <b><a name='main'></a>main</b><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>
<b>{</b>
    <font color='#009900'>// First let's make a 400x400 image.  This will form the input to the Hough transform.
</font>    array2d<font color='#5555FF'>&lt;</font><font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>char</u></font><font color='#5555FF'>&gt;</font> <font color='#BB00BB'>img</font><font face='Lucida Console'>(</font><font color='#979000'>400</font>,<font color='#979000'>400</font><font face='Lucida Console'>)</font>;
    <font color='#009900'>// Now we make a hough_transform object.  The 300 here means that the Hough transform
</font>    <font color='#009900'>// will operate on a 300x300 subwindow of its input image.  
</font>    hough_transform <font color='#BB00BB'>ht</font><font face='Lucida Console'>(</font><font color='#979000'>300</font><font face='Lucida Console'>)</font>;

    image_window win, win2;
    <font color='#0000FF'><u>double</u></font> angle1 <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
    <font color='#0000FF'><u>double</u></font> angle2 <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
    <font color='#0000FF'>while</font><font face='Lucida Console'>(</font><font color='#979000'>true</font><font face='Lucida Console'>)</font>
    <b>{</b>
        <font color='#009900'>// Generate a line segment that is rotating around inside the image.  The line is
</font>        <font color='#009900'>// generated based on the values in angle1 and angle2. So each iteration creates a
</font>        <font color='#009900'>// slightly different line.
</font>        angle1 <font color='#5555FF'>+</font><font color='#5555FF'>=</font> pi<font color='#5555FF'>/</font><font color='#979000'>130</font>;
        angle2 <font color='#5555FF'>+</font><font color='#5555FF'>=</font> pi<font color='#5555FF'>/</font><font color='#979000'>400</font>;
        <font color='#0000FF'>const</font> point cent <font color='#5555FF'>=</font> <font color='#BB00BB'>center</font><font face='Lucida Console'>(</font><font color='#BB00BB'>get_rect</font><font face='Lucida Console'>(</font>img<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;  
        <font color='#009900'>// A point 90 pixels away from the center of the image but rotated by angle1.
</font>        <font color='#0000FF'>const</font> point arc <font color='#5555FF'>=</font> <font color='#BB00BB'>rotate_point</font><font face='Lucida Console'>(</font>cent, cent <font color='#5555FF'>+</font> <font color='#BB00BB'>point</font><font face='Lucida Console'>(</font><font color='#979000'>90</font>,<font color='#979000'>0</font><font face='Lucida Console'>)</font>, angle1<font face='Lucida Console'>)</font>; 
        <font color='#009900'>// Now make a line that goes though arc but rotate it by angle2.
</font>        <font color='#0000FF'>const</font> point l <font color='#5555FF'>=</font> <font color='#BB00BB'>rotate_point</font><font face='Lucida Console'>(</font>arc, arc <font color='#5555FF'>+</font> <font color='#BB00BB'>point</font><font face='Lucida Console'>(</font><font color='#979000'>500</font>,<font color='#979000'>0</font><font face='Lucida Console'>)</font>, angle2<font face='Lucida Console'>)</font>;
        <font color='#0000FF'>const</font> point r <font color='#5555FF'>=</font> <font color='#BB00BB'>rotate_point</font><font face='Lucida Console'>(</font>arc, arc <font color='#5555FF'>-</font> <font color='#BB00BB'>point</font><font face='Lucida Console'>(</font><font color='#979000'>500</font>,<font color='#979000'>0</font><font face='Lucida Console'>)</font>, angle2<font face='Lucida Console'>)</font>;


        <font color='#009900'>// Next, blank out the input image and then draw our line on it.
</font>        <font color='#BB00BB'>assign_all_pixels</font><font face='Lucida Console'>(</font>img, <font color='#979000'>0</font><font face='Lucida Console'>)</font>;
        <font color='#BB00BB'>draw_line</font><font face='Lucida Console'>(</font>img, l, r, <font color='#979000'>255</font><font face='Lucida Console'>)</font>;

         
        <font color='#0000FF'>const</font> point <font color='#BB00BB'>offset</font><font face='Lucida Console'>(</font><font color='#979000'>50</font>,<font color='#979000'>50</font><font face='Lucida Console'>)</font>;
        array2d<font color='#5555FF'>&lt;</font><font color='#0000FF'><u>int</u></font><font color='#5555FF'>&gt;</font> himg;
        <font color='#009900'>// pick the window inside img on which we will run the Hough transform.
</font>        <font color='#0000FF'>const</font> rectangle box <font color='#5555FF'>=</font> <font color='#BB00BB'>translate_rect</font><font face='Lucida Console'>(</font><font color='#BB00BB'>get_rect</font><font face='Lucida Console'>(</font>ht<font face='Lucida Console'>)</font>,offset<font face='Lucida Console'>)</font>;
        <font color='#009900'>// Now let's compute the hough transform for a subwindow in the image.  In
</font>        <font color='#009900'>// particular, we run it on the 300x300 subwindow with an upper left corner at the
</font>        <font color='#009900'>// pixel point(50,50).  The output is stored in himg.
</font>        <font color='#BB00BB'>ht</font><font face='Lucida Console'>(</font>img, box, himg<font face='Lucida Console'>)</font>;
        <font color='#009900'>// Now that we have the transformed image, the Hough image pixel with the largest
</font>        <font color='#009900'>// value should indicate where the line is.  So we find the coordinates of the
</font>        <font color='#009900'>// largest pixel:
</font>        point p <font color='#5555FF'>=</font> <font color='#BB00BB'>max_point</font><font face='Lucida Console'>(</font><font color='#BB00BB'>mat</font><font face='Lucida Console'>(</font>himg<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
        <font color='#009900'>// And then ask the ht object for the line segment in the original image that
</font>        <font color='#009900'>// corresponds to this point in Hough transform space.
</font>        std::pair<font color='#5555FF'>&lt;</font>point,point<font color='#5555FF'>&gt;</font> line <font color='#5555FF'>=</font> ht.<font color='#BB00BB'>get_line</font><font face='Lucida Console'>(</font>p<font face='Lucida Console'>)</font>;

        <font color='#009900'>// Finally, let's display all these things on the screen.  We copy the original
</font>        <font color='#009900'>// input image into a color image and then draw the detected line on top in red.
</font>        array2d<font color='#5555FF'>&lt;</font>rgb_pixel<font color='#5555FF'>&gt;</font> temp;
        <font color='#BB00BB'>assign_image</font><font face='Lucida Console'>(</font>temp, img<font face='Lucida Console'>)</font>;
        <font color='#009900'>// Note that we must offset the output line to account for our offset subwindow.
</font>        <font color='#009900'>// We do this by just adding in the offset to the line endpoints. 
</font>        <font color='#BB00BB'>draw_line</font><font face='Lucida Console'>(</font>temp, line.first<font color='#5555FF'>+</font>offset, line.second<font color='#5555FF'>+</font>offset, <font color='#BB00BB'>rgb_pixel</font><font face='Lucida Console'>(</font><font color='#979000'>255</font>,<font color='#979000'>0</font>,<font color='#979000'>0</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
        win.<font color='#BB00BB'>clear_overlay</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
        win.<font color='#BB00BB'>set_image</font><font face='Lucida Console'>(</font>temp<font face='Lucida Console'>)</font>;
        <font color='#009900'>// Also show the subwindow we ran the Hough transform on as a green box.  You will
</font>        <font color='#009900'>// see that the detected line is exactly contained within this box and also
</font>        <font color='#009900'>// overlaps the original line.
</font>        win.<font color='#BB00BB'>add_overlay</font><font face='Lucida Console'>(</font>box, <font color='#BB00BB'>rgb_pixel</font><font face='Lucida Console'>(</font><font color='#979000'>0</font>,<font color='#979000'>255</font>,<font color='#979000'>0</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;

        <font color='#009900'>// We can also display the Hough transform itself using the jet color scheme.
</font>        win2.<font color='#BB00BB'>set_image</font><font face='Lucida Console'>(</font><font color='#BB00BB'>jet</font><font face='Lucida Console'>(</font>himg<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
    <b>}</b>
<b>}</b>


</pre></body></html>